package com.qiku.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiku.dao.UserMapper;
import com.qiku.domain.*;
import com.qiku.service.UserService;
import com.qiku.utils.Md5;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> findAllUserByPage(UserVo userVo) {
        PageHelper.startPage(userVo.getCurrentPage(),userVo.getPageSize());
        List<User> allUserByPage = userMapper.findAllUserByPage(userVo);
        PageInfo pageInfo = new PageInfo<>(allUserByPage);
        return pageInfo;
    }

    @Override
    public int updateUserStatus(Integer id, String status) {
        Date date = new Date();
        User user = new User();
        user.setUpdate_time(date);
        user.setStatus(status);
        user.setId(id);
        return userMapper.updateUserStatus(user);
    }

    @Override
    public User login(UserVo userVo) throws InvocationTargetException, IllegalAccessException {
        User user = new User();
        BeanUtils.copyProperties(user,userVo);
        User login = userMapper.login(user);
        //验证  密码是否正确
        if(null != login &&
                Md5.verify(user.getPassword(),"ak",login.getPassword())){
            return login;
        }
        return null;
    }

    @Override
    public int register(User user) {
        user.setCreate_time(new Date());
        user.setUpdate_time(new Date());
        int i = userMapper.register(user);
        return i;
    }

    @Override
    public List<Role> findUserRelationRoleById(Integer id) {
        return userMapper.findUserRelationRoleById(id);
    }

    @Override
    public void userContentRole(UserVo userVo) {
        //清空当前用户关联的角色
        userMapper.deleteUserContentRole(userVo.getUserId());
        //获取需要分配 角色的id集合
        for (Integer roleId : userVo.getRoleIdList()){
            User_Role_relation userRoleRelation = new User_Role_relation();
            userRoleRelation.setRoleId(roleId);
            userRoleRelation.setUserId(userVo.getUserId());
            //补全信息
            Date date = new Date();
            userRoleRelation.setCreatedTime(date);
            userRoleRelation.setUpdatedTime(date);
            userRoleRelation.setCreatedBy("system");
            userRoleRelation.setUpdatedby("system");
            userMapper.userContentRole(userRoleRelation);
        }

    }

    @Override
    public ResponseResult getUserPermissions(Integer id) {
        //根据id  获取当前用户角色列表
        List<Role> roleList = userMapper.findUserRelationRoleById(id);
        //将获取到的角色id存入list
        List<Integer> roleIdList = new ArrayList<>();
        for (Role role : roleList){
            roleIdList.add(role.getId());
        }
        //根据角色id  查询  父菜单
        List<Menu> parentMenuList = userMapper.findParentMenuByRoleId(roleIdList);
        //封装父菜单的下级菜单
        for (Menu menu : parentMenuList){
            //获取当前菜单的子菜单
            List<Menu> subMenuList = userMapper.findSubMenuByPid(menu.getId());
            menu.setSubMenuList(subMenuList);
        }
        //获取资源权限
        List<Resource> resourceList = userMapper.findResourceByRoleId(roleIdList);
        //封装数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("menuList",parentMenuList);
        map.put("resourceList",resourceList);
        ResponseResult result = new ResponseResult(true, 200, "响应成功~", map);
        return result;
    }
}
